package com.example.shopping_mall.base;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.example.shopping_mall.R;
import com.example.shopping_mall.fragment.IntroduceFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//商品介绍
public class ProductIntroductionActivity extends AppCompatActivity {

    private ArrayList<String> mTabList=new ArrayList<>(
            Arrays.asList("商品介绍","图文详情","商品评价","相关专题"));

    private List<IntroduceFragment> introduceFragments=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_introduction);

        initView();
    }

    private void initView() {
        TabLayout table=findViewById(R.id.table);
        ViewPager newsPager=findViewById(R.id.news_pager);

        for(int i=0;i<mTabList.size();i++){
            //循环加入Fragment，使每个标题都有其对应的内容
            introduceFragments.add(new IntroduceFragment());
        }

        //初始化TabLayout
        table.addTab(
                table.newTab()
                        .setText("分类标题")
                        .setTabLabelVisibility(TabLayout.TAB_LABEL_VISIBILITY_LABELED));

        NewsPagerAdapter adapter=new NewsPagerAdapter(
                getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

        newsPager.setAdapter(adapter);

        table.setupWithViewPager(newsPager);
    }

    class NewsPagerAdapter extends FragmentPagerAdapter {

        public NewsPagerAdapter(FragmentManager fm, int behavior){
            super(fm,behavior);

        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return introduceFragments.get(position);
        }

        //获取viewpager中有多少个view
        @Override
        public int getCount() {
            return mTabList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTabList.get(position);
        }
    }
}